<?php

class Login extends Page {

    protected $allowedUsers = array();
    protected $template = 'login.tpl';
    protected $customTemplate = true;

    public function __construct() {
        parent::__construct();
        $action = App::getRequest('action', 'none');
        switch ($action) {
            case "login":
                $this->checkLogin();
                break;
            case "logout":
                $this->logout();
                break;
        }
    }

    function checkLogin() {
        $myusername = $_POST['myusername'];
        $mypassword = $_POST['mypassword'];
        $persistent = isset($_POST['rememberme']);

        $user = new User();
        if ($user->login($myusername, $mypassword, $persistent)) {
            $_SESSION['user'] = $user;
            App::redirect('Main');
        } else {
            $this->smarty->assign('loginFailed', true);
        }
    }

    public function logout() {
        User::logout();
        session_unset();
        session_destroy();
        header("Location: /");
        die();
    }

}

?>